package com.controller;


import com.common.utils.PageUtils;
import com.common.utils.R;
import com.entity.*;
import com.service.*;
import com.vo.PaperInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.NameParser;
import java.awt.print.Paper;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 试卷管理表
 *
 * @author lxl
 * @email lixialang007@163.com
 * @date 2021-09-08 21:36:19
 */
@RestController
@RequestMapping("exam/papermanage")
public class PaperManageController {
    @Autowired
    private PaperManageService paperManageService;

    @Autowired
    private MultiQuestionService multiQuestionService;//选择题

    @Autowired
    private FillQuestionService fillQuestionService;//填空题

    @Autowired
    private JudgeQuestionService judgeQuestionService;//判断题

    @Autowired
    private ExamManageService examManageService;//考试管理


    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("exam:papermanage:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = paperManageService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/paperList")
    public R paperList() {
        List<PaperManageEntity> paperList = paperManageService.getPaperList();
        List<MultiQuestionEntity> multiList = multiQuestionService.getAllList();//选择题
        List<FillQuestionEntity> fillList = fillQuestionService.getAllList();//填空题
        List<JudgeQuestionEntity> judgeList = judgeQuestionService.getAllList();//判断题
        List<ExamManageEntity> examList = examManageService.getAllList();


        Map<Integer, MultiQuestionEntity> multis = new HashMap<>();
        for (MultiQuestionEntity multi : multiList) {
            multis.put(multi.getQuestionid(), multi);
        }
        Map<Integer, FillQuestionEntity> fills = new HashMap();
        for (FillQuestionEntity fill : fillList) {
            fills.put(fill.getQuestionid(), fill);
        }
        Map<Integer, JudgeQuestionEntity> judges = new HashMap<>();
        for (JudgeQuestionEntity judge : judgeList) {
            judges.put(judge.getQuestionid(), judge);
        }

        Map<Integer, String> exams = examList.stream()
                .collect(Collectors.toMap(ExamManageEntity::getPaperid, ExamManageEntity::getSource));
        List<PaperInfo> paperInfos = new ArrayList<>();
        for (PaperManageEntity paper : paperList) {
            PaperInfo paperInfo = new PaperInfo();
            paperInfo.setId(paper.getId());
            paperInfo.setPaperId(paper.getPaperid());
            paperInfo.setPaperName(exams.get(paper.getPaperid()));
            paperInfo.setQuestionType(paper.getQuestiontype());
            if (paper.getQuestiontype() == 1) {
                paperInfo.setQuestion(multis.get(paper.getQuestionid()));
            } else if (paper.getQuestiontype() == 2) {
                paperInfo.setQuestion(fills.get(paper.getQuestionid()));
            } else if (paper.getQuestiontype() == 3) {
                paperInfo.setQuestion(judges.get(paper.getQuestionid()));
            }
            paperInfos.add(paperInfo);
        }
        Set<Integer> paperIdList = paperList.stream().map(PaperManageEntity::getPaperid)
                .collect(Collectors.toSet());
        List<PaperInfo> res = new ArrayList<>();
        for (Integer integer : paperIdList) {
            List<PaperInfo> collect = paperInfos.stream().
                    filter(paperInfo -> paperInfo.getPaperId().equals(integer))
                    .sorted(Comparator.comparingInt(x1 -> x1.getQuestionType()))
                    .collect(Collectors.toList());
            PaperInfo p = new PaperInfo();
            p.setPaperId(collect.get(0).getPaperId());
            p.setPaperName(collect.get(0).getPaperName());
            p.setChildren(collect);
            res.add(p);
        }

        return R.ok().put("paperInfos", res);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//   @RequiresPermissions("exam:papermanage:info")
    public R info(@PathVariable("id") Integer id) {
        PaperManageEntity paperManage = paperManageService.getById(id);

        return R.ok().put("paperManage", paperManage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//  @RequiresPermissions("exam:papermanage:save")
    public R save(@RequestBody PaperManageEntity paperManage) {
        paperManageService.save(paperManage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("exam:papermanage:update")
    public R update(@RequestBody PaperManageEntity paperManage) {
        paperManageService.updateById(paperManage);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("exam:papermanage:delete")
    public R delete(@RequestBody Integer[] ids) {
        paperManageService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
